package com.haierCamera.customapplication.ui.main;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklAdapterRecordItem1Binding;
import com.haierCamera.customapplication.ui.business.entity.RecordInfo;
import com.haierCamera.customapplication.ui.custom.BaseListenerOnClik;
import com.haierCamera.customapplication.ui.custom.DataBoundViewHolder;
import com.haierCamera.customapplication.utils.DensityUtil;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

public class RecordListHorAdapter extends RecyclerView.Adapter<DataBoundViewHolder<HzklAdapterRecordItem1Binding>> {

    private List<RecordInfo> data;
    private Context context;
    private FragmentActivity activity;
    private BaseListenerOnClik click;
    private boolean isEdit;

    public RecordListHorAdapter(Context context) {
        this.context = context;
    }

    public void setActivity(FragmentActivity activity) {
        this.activity = activity;
    }

    public void upDate(List<RecordInfo> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setOnItemOnClick(BaseListenerOnClik click) {
        this.click = click;
    }

    @Override
    public DataBoundViewHolder<HzklAdapterRecordItem1Binding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        Log.i("TAG", "LeaveMessageAdapter_onCreateViewHolder");
        HzklAdapterRecordItem1Binding binding = DataBindingUtil.inflate(LayoutInflater.from(context)
                , R.layout.hzkl_adapter_record_item_1, viewGroup, false);
        return new DataBoundViewHolder<>(binding);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull DataBoundViewHolder<HzklAdapterRecordItem1Binding> holder, int i) {
//        holder.binding.tvName.setText(data.get(i).getName());

        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        Log.i("TAG", "width-" + width);
        int imgWidth = (width- DensityUtil.dip2px(context,8) * 2 ) /3;
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) holder.binding.ivRecord.getLayoutParams();
        params.width = imgWidth;
        holder.binding.ivRecord.setLayoutParams(params);

        if (i == data.size()) {
            holder.binding.ivRecord.setImageResource(R.drawable.hzkl_record_more);
        }
        holder.itemView.setOnClickListener((v) -> {
            click.onClick(i);
        });


    }

    @Override
    public int getItemCount() {
        if (data != null) {
            return data.size() + 1;
        } else {
            return 0;
        }
    }
}